package cn.meizhu.quartz.job;

import cn.meizhu.base.canstant.OrderCanstant;
import cn.meizhu.base.canstant.PayCanstant;
import cn.meizhu.order.domain.OrderAdopt;
import cn.meizhu.order.domain.OrderProduct;
import cn.meizhu.order.mapper.OrderAdoptMapper;
import cn.meizhu.order.mapper.OrderProductMapper;
import cn.meizhu.pay.domain.PayAlipayInfo;
import cn.meizhu.pay.domain.PayBill;
import cn.meizhu.pay.mapper.PayAliPayInfoMapper;
import cn.meizhu.pay.mapper.PayBillMapper;
import cn.meizhu.pet.domain.Pet;
import cn.meizhu.pet.mapper.PetMapper;
import cn.meizhu.serve.domain.Product;
import cn.meizhu.serve.mapper.ProductMapper;
import cn.meizhu.util.PayAliPayUtil;
import org.mockito.internal.matchers.Or;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.Map;

public class QuartzJob implements Job {

    @Autowired
    private PayBillMapper payBillMapper;

    @Autowired
    private OrderAdoptMapper orderAdoptMapper;

    @Autowired
    private OrderProductMapper orderProductMapper;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private PayAliPayInfoMapper payAliPayInfoMapper;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        JobDataMap jobDataMap = jobExecutionContext.getMergedJobDataMap();
        Map<String,Object> params = (Map<String, Object>) jobDataMap.get("params");
        String orderSn = (String) params.get("orderSn");
        PayBill payBill = payBillMapper.findByOrderSn(orderSn);
        payBill.setState(-1);
        payBillMapper.update(payBill);
        switch (payBill.getBusinessType()){
            case OrderCanstant.ORDER_ADOPT:
                //根据业务类型  找到对应的订单
                OrderAdopt orderAdopt = orderAdoptMapper.findById(payBill.getBusinessKey());
                //设置订单的状态， 支付单号
                orderAdopt.setState(-1);
                orderAdoptMapper.update(orderAdopt);
                //上架宠物
                petMapper.onsale(Arrays.asList(orderAdopt.getPet_id()));
                Long pet_id = orderAdopt.getPet_id();
                Pet pet = petMapper.findById(pet_id);
                pet.setState(-1);
                petMapper.update(pet);
                break;
            case OrderCanstant.ORDER_PRODUCT:
                //根据业务类型  找到对应的订单
                OrderProduct orderProduct = orderProductMapper.findById(payBill.getBusinessKey());
                //设置订单的状态， 支付单号
                orderProduct.setState(-1);
                orderProductMapper.update(orderProduct);
                //上架宠物
                petMapper.onsale(Arrays.asList(orderProduct.getProduct_id()));
                Long product_id = orderProduct.getProduct_id();
                Product product = productMapper.findById(product_id);
                product.setSalecount(product.getSalecount()-1);
                productMapper.update(product);
                break;
            case OrderCanstant.ORDER_GOODS:
                break;
        }


    }
}